CREATE TABLE IF NOT EXISTS`__Address`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`OwnerID` BIGINT(20) UNSIGNED NOT NULL,
	`AddressLine1` VARCHAR(50) NULL,
	`AddressLine2` VARCHAR(50) NULL,
	`Town` VARCHAR(50) NULL,
	`Code` VARCHAR(50) NULL,
	`Country` VARCHAR(50) NULL,
	`Stamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`Visible` BOOL NOT NULL,
	 PRIMARY KEY(`Id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `__Category`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`ParentID` BIGINT(20) UNSIGNED NOT NULL,
	`Name` VARCHAR(50) NULL,
	`Description` VARCHAR(50) NULL,
	`Stamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`Visible` BOOL NOT NULL,
	 PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `__Configuration`
(
	`Title` VARCHAR(50) NOT NULL,
	`Value` BLOB NULL,
	`Description` VARCHAR(50) NULL,
	 PRIMARY KEY(`Title`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `__Customer`
(
	`Id` VARCHAR(50) NOT NULL,
	`Firstname` VARCHAR(50) NULL,
	`Surname` VARCHAR(50) NULL,
	`Stamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`Visible` BOOL NOT NULL,
	 PRIMARY KEY(`Id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS`__Email`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`OwnerID` BIGINT(20) UNSIGNED NOT NULL,
	`EmailAddress` VARCHAR(50) NULL,
	`Stamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`Visible` BOOL NOT NULL,
	 PRIMARY KEY(`Id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `__Group`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`Name` VARCHAR(50) NOT NULL,
	`Description` LONGTEXT NULL,
	`Stamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`Visible` BOOL NOT NULL,
	 PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `__Order`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`OwnerID` BIGINT(20) UNSIGNED NOT NULL,
	`OrderDate` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`DeliveryDate` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`Status` VARCHAR(50) NULL,
	`Comment` LONGTEXT NULL,
	`Stamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`Visible` BOOL NOT NULL,
	 PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `__OrderDetail`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`OrderId` BIGINT(20) UNSIGNED NOT NULL,
	`ProductId` BIGINT(20) UNSIGNED NOT NULL,
	`Quantity` INT NOT NULL,
	`UnitCost` DECIMAL NOT NULL,
	`Stamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`Visible` BOOL NOT NULL,
	 PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS`__Phone`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`OwnerID` BIGINT(20) UNSIGNED NOT NULL,
	`Number` VARCHAR(50) NULL,
	`Type` VARCHAR(50) NULL,
	`Stamp` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
	`Visible` BOOL NOT NULL,
	 PRIMARY KEY(`Id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `__ApplicationAudit`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`Action` VARCHAR(50) NULL,
	`Description` VARCHAR(50) NULL,
	`Status` VARCHAR(50) NULL,
	`SourceID` BIGINT NULL,
	`DestinationID` BIGINT NULL,
	`Stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	`Workstation` VARCHAR(255) NULL,
	`Domain` VARCHAR(255) NULL,
	`WinUser` VARCHAR(255) NULL,
	 PRIMARY KEY(`Id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `__SystemAudit`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`Action` VARCHAR(50) NULL,
	`Description` VARCHAR(50) NULL,
	`Status` VARCHAR(50) NULL,
	`SourceID` BIGINT NULL,
	`Stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	`Workstation` VARCHAR(255) NULL,
	`Domain` VARCHAR(255) NULL,
	`WinUser` VARCHAR(255) NULL,
	 PRIMARY KEY(`Id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `__UserSetting`
(
	`UserID` BIGINT NOT NULL,
	`Setting` VARCHAR(50) NOT NULL,
	`Value` BLOB NULL,
	 PRIMARY KEY(`UserID`,`Setting`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `__User`
(
	`Id` BIGINT(20) UNSIGNED  AUTO_INCREMENT NOT NULL,
	`Username` VARCHAR(50) NOT NULL,
	`Password` LONGTEXT NULL,
	`IsLocked` BOOL NOT NULL,
	`IsActive` BOOL NOT NULL,
	`ResetQuestion` VARCHAR(50) NULL,
	`ResetAnswer` LONGTEXT NULL,
	`Stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
	 PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

CREATE TABLE IF NOT EXISTS `__UserGroup`
(
	`UserID` BIGINT NOT NULL,
	`GroupID` BIGINT NOT NULL,
	 PRIMARY KEY(`UserID`,`GroupID`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `__UserRight`
(
	`UserID` BIGINT NOT NULL,
	`RightCode` VARCHAR(50) NOT NULL,
	`CabinetID` BIGINT NOT NULL,
	 PRIMARY KEY(`UserID`,`RightCode`,`CabinetID`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `__GroupRight`
(
	`GroupID` BIGINT NOT NULL,
	`RightCode` VARCHAR(50) NOT NULL,
	`CabinetID` BIGINT NOT NULL,
	 PRIMARY KEY(`GroupID`,`RightCode`,`CabinetID`)
)ENGINE=INNODB DEFAULT CHARSET=latin1;

